package com.hmy.finance.service.receipt.dto;

import cn.hutool.core.collection.CollectionUtil;
import com.hmy.ccp.common.dto.BaseDto;
import com.hmy.finance.api.bo.core.request.enums.OffsetVoucherStatusEnum;
import com.hmy.finance.api.bo.core.request.enums.ReceiptOffsetBillStatusEnum;
import com.hmy.finance.dal.po.json.VoucherAttachmentsJson;
import com.hmy.finance.dal.po.receipt.ReceiptVoucherPo;
import com.hmy.finance.dal.po.receipt.json.AccountInfoJson;
import com.hmy.finance.dal.po.receipt.json.CustomerContactorIAccountInfoJson;
import com.hmy.finance.dal.po.receipt.json.CustomerContactorJson;
import com.hmy.finance.dal.po.receipt.json.CustomerInfoJson;
import com.hmy.finance.dal.po.receipt.ReceiptContractApportionRecordPo;
import com.hmy.finance.dal.po.receipt.ReceiptOffsetBillPo;
import com.hmy.finance.service.receipt.dto.receiptOffset.ContactorApportionDto;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Data
public class ReceiptOffsetBillCreateDto extends BaseDto {

    /**
     * 核心单id
     */
    private Long receiptOffsetBillId;

    /**
     * 核销单No
     */
    private String receiptOffsetBillNo;

    /**
     * 流程id
     */
    private String processInstanceId;

    /**
     * 客户id
     */
    private Long customerId;

    /**
     * 客户名称
     */
    private String customerName;

    /**
     * 销售经理
     */
    private Long saleManagerId;

    /**
     * 销售经理名称
     */
    private String saleManagerName;

    /**
     * 销售经理id
     */
    private Long saleManagerDeptId;

    /**
     * 联系人
     */
    private List<CustomerContactorDto> customerContactors;

    /**
     * 收款日期
     */
    private LocalDateTime receiptedTime;

    /**
     * 收款金额
     */
    private BigDecimal receiptedAmount;

    /**
     * 收款类型code
     */
    private Integer receiptedType;

    /**
     * 凭证信息
     */
    private ReceiptVoucherDto receiptVoucher;

    /**
     * 客户账户余额
     */
    private BigDecimal customerBalanceAmount;

    /**
     * 客户总欠款金额
     */
    private BigDecimal customerTotalArrearsAmount;

    /**
     * 客户本次核销金额(此核销单核销金额)
     */
    private BigDecimal customerOffsetAmount;

    /**
     * 客户核销后总欠款金额
     */
    private BigDecimal customerArrearsAmountAfterOffset;

    /**
     * 联系人分摊明细。
     */
    private List<ContactorApportionDto> contactorApportionInfos;

    /**
     * 备注
     */
    private String remarks;

    /**
     * 创建receiptOffsetBillPo对象
     * 核销单主表
     */
    public void dto2ReceiptOffsetBillPoBuilder(ReceiptOffsetBillPo receiptOffsetBillPo) {
        receiptOffsetBillPo.setReceiptOffsetBillId(this.getReceiptOffsetBillId());
        receiptOffsetBillPo.setReceiptOffsetBillNo(this.getReceiptOffsetBillNo());
        receiptOffsetBillPo.setCustomerId(this.getCustomerId());
        receiptOffsetBillPo.setCustomerName(this.getCustomerName());
        receiptOffsetBillPo.setSaleManagerId(this.getSaleManagerId());
        receiptOffsetBillPo.setSaleManagerName(this.getSaleManagerName());
        receiptOffsetBillPo.setProcessInstanceId(this.processInstanceId);
        receiptOffsetBillPo.setBillStatus(ReceiptOffsetBillStatusEnum.AREA_MANAGER_APPROVE_ING.getStatusCode());

        /*客户信息封装*/
        CustomerInfoJson customerInfoJson = new CustomerInfoJson();
        customerInfoJson.setCustomerId(this.getCustomerId());
        customerInfoJson.setCustomerName(this.getCustomerName());
        receiptOffsetBillPo.setCustomerExtensionInfo(customerInfoJson);

        /*联系人信息封装*/
        List<CustomerContactorJson> customerContactorExtensionInfos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(this.getContactorApportionInfos())) {
            this.getContactorApportionInfos().forEach(customerContactorDto -> {
                CustomerContactorJson customerContactorJson = new CustomerContactorJson();
                customerContactorJson.setCustomerId(this.getCustomerId());
                customerContactorJson.setCustomerName(this.getCustomerName());
                customerContactorJson.setCustomerContactorId(customerContactorDto.getCustomerContactorId());
                customerContactorJson.setCustomerContactorName(customerContactorDto.getCustomerContactorName());
                customerContactorJson.setContactorTotalArrearsAmount(customerContactorDto.getContactorTotalArrearsAmount());//联系人目前总欠款金额
                customerContactorJson.setBalanceAmount(customerContactorDto.getBalanceAmount());//联系人目前余额
                customerContactorJson.setApportionAmount(customerContactorDto.getApportionAmount());//联系人本次分摊金额
                customerContactorExtensionInfos.add(customerContactorJson);
            });
        }
        receiptOffsetBillPo.setCustomerContactorExtensionInfos(customerContactorExtensionInfos);

        /*账户信息封装*/
        AccountInfoJson accountInfoJson = new AccountInfoJson();
        accountInfoJson.setCustomerId(this.getCustomerId());
        accountInfoJson.setCustomerName(this.getCustomerName());
        accountInfoJson.setCustomerTotalArrearsAmount(this.getCustomerTotalArrearsAmount());
        accountInfoJson.setCustomerOffsetAmount(this.getCustomerOffsetAmount());
        accountInfoJson.setCustomerArrearsAmountAfterOffset(this.getCustomerArrearsAmountAfterOffset());
        accountInfoJson.setCustomerBalanceAmount(this.getCustomerBalanceAmount());
        List<CustomerContactorIAccountInfoJson> customerContactorAmountInfo = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(this.getContactorApportionInfos())) {
            this.getContactorApportionInfos().forEach(contactorApportionDto -> {
                CustomerContactorIAccountInfoJson customerContactorIAccountInfoJson = new CustomerContactorIAccountInfoJson();
                customerContactorIAccountInfoJson.setCustomerContactorId(contactorApportionDto.getCustomerContactorId());
                customerContactorIAccountInfoJson.setCustomerContactorName(contactorApportionDto.getCustomerContactorName());
                customerContactorIAccountInfoJson.setBalanceAmount(contactorApportionDto.getBalanceAmount());
                customerContactorIAccountInfoJson.setApportionAmount(contactorApportionDto.getApportionAmount());
                customerContactorAmountInfo.add(customerContactorIAccountInfoJson);
            });

        }
        accountInfoJson.setCustomerContactorAmountInfo(customerContactorAmountInfo);

        receiptOffsetBillPo.setVoucherId(this.getReceiptVoucher().getVoucherId());
        receiptOffsetBillPo.setAccountExtensionInfos(accountInfoJson);
        receiptOffsetBillPo.setIsApportion(CollectionUtil.isNotEmpty(this.getCustomerContactors()) ? 1 : 0);
        receiptOffsetBillPo.setRemarks(this.getRemarks());
        receiptOffsetBillPo.setReceiptedTime(this.getReceiptedTime());
        receiptOffsetBillPo.setReceiptedAmount(this.getReceiptedAmount());
        receiptOffsetBillPo.setReceiptedType(this.getReceiptedType());
        receiptOffsetBillPo.setSaleManagerId(this.saleManagerId);
        receiptOffsetBillPo.setSaleManagerName(this.saleManagerName);
        receiptOffsetBillPo.setSaleManagerDeptId(this.getSaleManagerDeptId());
        receiptOffsetBillPo.setCreatorId(this.getCreatorId());
        receiptOffsetBillPo.setCreatorName(this.getCreatorName());
        receiptOffsetBillPo.setUpdaterId(this.getUpdaterId());
        receiptOffsetBillPo.setUpdaterName(this.getUpdaterName());
    }

    /**
     * 创建receiptContractApportionRecordPo对象
     * 核销单合同分摊表
     */
    public void dto2ReceiptContractApportionRecordPoBuilder(ContactorApportionDto contactorApportionDto, List<ReceiptContractApportionRecordPo> receiptContractApportionRecordPos) {
        if (CollectionUtil.isNotEmpty(contactorApportionDto.getContractApportions())) {
            contactorApportionDto.getContractApportions().forEach(contractApportionDto -> {
                ReceiptContractApportionRecordPo receiptContractApportionRecordPo = new ReceiptContractApportionRecordPo();
                receiptContractApportionRecordPo.setReceiptOffsetBillId(this.receiptOffsetBillId);
                receiptContractApportionRecordPo.setContractId(contractApportionDto.getContractId());
                receiptContractApportionRecordPo.setContractNo(contractApportionDto.getContractNo());

                /*客户信息封装*/
                CustomerContactorJson customerContactorJson = new CustomerContactorJson();
                customerContactorJson.setCustomerId(this.getCustomerId());
                customerContactorJson.setCustomerName(this.getCustomerName());
                customerContactorJson.setCustomerContactorId(contactorApportionDto.getCustomerContactorId());
                customerContactorJson.setCustomerContactorName(contactorApportionDto.getCustomerContactorName());
                customerContactorJson.setContactorTotalArrearsAmount(contactorApportionDto.getContactorTotalArrearsAmount());//联系人目前总欠款金额
                customerContactorJson.setBalanceAmount(contactorApportionDto.getBalanceAmount());//联系人目前余额
                customerContactorJson.setApportionAmount(contactorApportionDto.getApportionAmount());//联系人本次分摊金额
                customerContactorJson.setContactorArrearsAmountAfterOffset(contactorApportionDto.getContactorArrearsAmountAfterOffset());//联系人核销后总欠款金额
                receiptContractApportionRecordPo.setCustomerContactorExtensionInfo(customerContactorJson);

                receiptContractApportionRecordPo.setCustomerId(this.getCustomerId());
                receiptContractApportionRecordPo.setCustomerName(this.getCustomerName());
                receiptContractApportionRecordPo.setCustomerContactorId(contactorApportionDto.getCustomerContactorId());
                receiptContractApportionRecordPo.setCustomerContactorName(contactorApportionDto.getCustomerContactorName());
                receiptContractApportionRecordPo.setTotalArrearsAmount(contractApportionDto.getContractTotalArrearsAmount());
                receiptContractApportionRecordPo.setSuggestOffsetAmount(contractApportionDto.getContractSuggestOffsetAmount());
                receiptContractApportionRecordPo.setActualOffsetAmount(contractApportionDto.getContractActualOffsetAmount());
                receiptContractApportionRecordPo.setOffsetedArrearsAmount(contractApportionDto.getContractArrearsAmountAfterOffset());
                receiptContractApportionRecordPo.setCreatorId(this.getCreatorId());
                receiptContractApportionRecordPo.setCreatorName(this.getCreatorName());
                receiptContractApportionRecordPo.setUpdaterId(this.getUpdaterId());
                receiptContractApportionRecordPo.setUpdaterName(this.getUpdaterName());
                receiptContractApportionRecordPos.add(receiptContractApportionRecordPo);
            });
        }
    }

    public void dto2ReceiptVoucherPoBuilder(ReceiptVoucherPo receiptVoucherPo) {
        receiptVoucherPo.setId(this.getReceiptVoucher().getVoucherId());
        receiptVoucherPo.setVoucherNo(this.getReceiptVoucher().getVoucherNo());
        receiptVoucherPo.setOffsetAmount(this.getCustomerOffsetAmount());
        receiptVoucherPo.setUnOffsetAmount(BigDecimal.ZERO);//目前为全核销
        receiptVoucherPo.setReceiptDate(this.getReceiptedTime());
        receiptVoucherPo.setPaymentType(this.receiptedType);
        receiptVoucherPo.setOffsetStatus(OffsetVoucherStatusEnum.OFFSETED);

        List<VoucherAttachmentsJson> attachments = new ArrayList<>();
        this.getReceiptVoucher().getOssUrl().forEach(o -> {
            VoucherAttachmentsJson voucherAttachment = new VoucherAttachmentsJson();
            voucherAttachment.setOssUrl(o);
            attachments.add(voucherAttachment);
        });
        receiptVoucherPo.setAttachments(attachments);
        receiptVoucherPo.setCreatorId(this.getCreatorId());
        receiptVoucherPo.setCreatorName(this.getCreatorName());
        receiptVoucherPo.setUpdaterId(this.getUpdaterId());
        receiptVoucherPo.setUpdaterName(this.getUpdaterName());
    }

}
